<template>
  <div style="padding:10px;">
    <el-row>
      <el-button type="primary" @click="$router.go(-1)">返回</el-button>
      <el-input placeholder="请输入内容" style="float:right; width:90%" />
    </el-row>
    <h2>溯源信息公示</h2>
    <el-table
      v-loading="loading"
      :data="porks"
      stripe
      style="width: 100%"
    >
      <el-table-column
        prop="id"
        label="ID"
        width="60"
      />
      <el-table-column
        prop="part_name"
        label="部位"
      />
      <el-table-column
        prop="weight"
        label="重量 (/kg)"
      />
      <el-table-column
        prop="enterprise.address"
        label="产地"
      />
      <el-table-column
        prop="cut_date"
        label="加工时间"
      />
      <el-table-column
        fixed="right"
        label="操作"
        width="150"
        align="center"
      >
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="trace(scope.row)">溯源</el-button>
          <el-button size="small" type="primary" @click="showQRCode(scope.row)">QR</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="溯源二维码" :visible.sync="QRvisible" style="text-align:center;">
      <vue-qr :text="QRCodeInfo.url" :size="500" />
      <div style="text-align:center; margin-top:20px;">
        <el-button type="primary" @click="printQR">打印</el-button>
        <el-button type="info" @click="QRvisible = false">关闭</el-button>
      </div>
    </el-dialog>

    <div class="block" style="text-align:center;">
      <el-pagination
        :current-page.sync="currentPage"
        :page-size="pagesize"
        :page-sizes="[10, 15, 30, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
    </div>
  </div>
</template>

<script>
import { traceList } from '@/api/trace'
import moment from 'moment'
import vueQr from 'vue-qr'

export default {
  components: {
    vueQr
  },
  data() {
    return {
      QRCodeInfo: {
        url: ''
      },
      porks: [],
      loading: true,
      currentPage: 1,
      pagesize: 15,
      total: 0,
      QRvisible: false
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      traceList({ current: 1, size: 15 }).then((response) => {
        this.porks = response.obj.records
        this.porks.forEach((x) => { x.cut_date = moment(x.cut_date).format('YYYY-MM-DD HH:mm:ss') })
        this.total = response.obj.total
        this.loading = false
      })
    },
    handleSizeChange(val) {
      this.pagesize = val
      this.handleCurrentChange(val)
    },
    handleCurrentChange(val) {
      this.porks = null
      this.loading = true
      traceList({ current: this.currentPage, size: this.pagesize }).then(response => {
        this.porks = response.obj.records
        this.total = response.obj.total
        this.porks.forEach((x) => { x.cut_date = moment(x.cut_date).format('YYYY-MM-DD HH:mm:ss') })
        this.loading = false
      })
    },
    trace(row) {
      this.$router.push({ path: '/trace', query: { id: row.id }})
    },
    showQRCode(row) {
      this.QRvisible = true
      this.QRCodeInfo.url = 'http://1.117.225.11:80/#/trace?id=' + row.id
    },
    printQR() {
      this.$message('打印二维码操作')
    }
  }
}
</script>
